/*
 * BlackjackGame.java
 *
 * Created on September 5, 2007, 8:37 PM
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package blackjacksim;

import java.util.*;

/**
 *
 * @author Eric Drucker
 */
public class BlackjackGame
{
	
	private int money;
	private int betAmount;
	private int playerScore;
	private int computerScore;
	
	private Deck deck;
	
        private boolean finished;
	private boolean gameInProgress;
	private boolean bust;
	
	private ArrayList<Card> computerHand;
	private ArrayList<Card> playerHand;
	
	private String endText;

	/** Creates a new instance of BlackjackGame */
	public BlackjackGame()
	{
		money = 5000;
		betAmount = 0;
		gameInProgress = false;
                finished = false;
		
		deck = new Deck();
		
		computerHand = new ArrayList<Card>();
		playerHand = new ArrayList<Card>();
	}
        
        public ArrayList<Card> getComputerHand()
	{
		return computerHand;
	}
	
	public ArrayList<Card> getPlayerHand()
	{
		return playerHand;
	}
	
	public void setGameInProgress(boolean g)
	{
		gameInProgress = g;
	}
	
	public boolean getGameInProgress()
	{
		return gameInProgress;
	}
        
        public boolean getFinished()
	{
		return finished;
	}
        
        public String getResults()
	{
		return endText;
	}
	
	public void setMoney(int m)
	{
		money = m;
	}
	
	public int getMoney()
	{
		return money;
	}
	
	public void setBetAmount(int b)
	{
		betAmount = b;
	}
	
	public int getBetAmount()
	{
		return betAmount;
	}
	
	public int getPlayerScore()
	{
		return playerScore;
	}
	
	public int getComputerScore()
	{
                calcComputerScore();
		return computerScore;
	}
	
	
	public void bet(int b)
	{
		if(deck.numCardsLeft() < 15)
		{
			deck.shuffle();
		}
		
		bust = false;
		
		betAmount = b;
		
		money -= betAmount;
		
		computerHand = new ArrayList<Card>();
		playerHand = new ArrayList<Card>();
		
		computerHand.add(deck.deal());
		playerHand.add(deck.deal());
		computerHand.add(deck.deal());
		playerHand.add(deck.deal());
                
                calcPlayerScore();
                System.out.println(getPlayerScore());
                		
		gameInProgress = true;
	}
	
	public void hit()
	{
		playerHand.add(deck.deal());
		
		calcPlayerScore();
                System.out.println(getPlayerScore());
	}
	
	public void stand()
	{
		gameInProgress = false;
		calcPlayerScore();
		finishGame();
	}

	private void calcPlayerScore()
	{
            playerScore = Score(playerHand);
            if (playerScore > 21)
            {
		bust = true;
                System.out.println("busted");
		gameInProgress = false;
                finishGame();
            }
        }
	
	private void calcComputerScore()
	{
            computerScore = Score(computerHand);
            
            if(computerScore < 17)
            {
                computerHand.add(deck.deal());
                calcComputerScore();
            }
        }
        
        private int Score(ArrayList<Card> hand)
        {
            int score = 0;
            int numAces = 0;
		
		for(Card c: hand)
		{
			Card.value v = c.getValue();
			
			switch(v)
			{
				case TWO:	score += 2; break;
				case THREE:	score += 3; break;
				case FOUR:	score += 4; break;
				case FIVE:	score += 5; break;
				case SIX:	score += 6; break;
				case SEVEN:	score += 7; break;
				case EIGHT:	score += 8; break;
				case NINE:	score += 9; break;
				case TEN:	score += 10; break;
				case JACK:	score += 10; break;
				case QUEEN:	score += 10; break;
				case KING:	score += 10; break;
				case ACE:	numAces++;
			}
		}
		
		if(score + numAces*11 <= 21)
		{
			score += numAces*11;
		}
		else
		{
			score += numAces;
		}
                        
            return score;
        }
        
        public void nextGame()
        {
            computerHand = new ArrayList<Card>();
            playerHand = new ArrayList<Card>();
            finished = false;
        }

	private void finishGame()
	{
            
            if(bust || (playerScore < getComputerScore() && computerScore < 22))
                endText = "YOU LOSE";
            else if(playerScore > computerScore || computerScore > 21)
                {
                        money += 2*(betAmount);
                        endText = "YOU WIN";
                }
                else 
                {
                        money += betAmount;
                        endText = "PUSH";
                }
            
            System.out.println("CS:" + computerScore);                    
		
            /*
                 if(!bust && playerScore > computerScore)
		{
                     
			money += 2*(betAmount);
			endText = "YOU WIN";
		}
		else if(!bust && playerScore == computerScore)
		{
			money += betAmount;
			endText = "PUSH";
		}
		else
		{
			endText = "YOU LOSE";
		}
             */
                
            gameInProgress = false;
            System.out.println(endText);
            finished = true;
	}
}
